{% extends "base.html" %}

{% block title %}系统管理 - 校园智能生活助手{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="row">
        <div class="col-12 text-center">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2><i class="fas fa-cog"></i> 系统管理</h2>
                <a href="{{ url_for('dashboard') }}" class="btn btn-outline-primary">
                    <i class="fas fa-home"></i> 返回主页
                </a>
            </div>
            <h2 class="admin-title"><i class="fas fa-cog"></i> 系统管理面板</h2>
            <p class="admin-subtitle">管理员：{{ session.name }}</p>
        </div>
    </div>

    <!-- 统计卡片 -->
    <div class="row mt-4">
        <div class="col-md-4 mb-3">
            <div class="card bg-primary text-white h-100">
                <div class="card-body text-center">
                    <i class="fas fa-users fa-3x mb-3"></i>
                    <h3 class="card-title">{{ student_count }}</h3>
                    <p class="card-text">注册学生数</p>
                </div>
            </div>
        </div>
        <div class="col-md-4 mb-3">
            <div class="card bg-success text-white h-100">
                <div class="card-body text-center">
                    <i class="fas fa-chair fa-3x mb-3"></i>
                    <h3 class="card-title">{{ reservation_count }}</h3>
                    <p class="card-text">座位预约数</p>
                </div>
            </div>
        </div>
        <div class="col-md-4 mb-3">
            <div class="card bg-warning text-white h-100">
                <div class="card-body text-center">
                    <i class="fas fa-search fa-3x mb-3"></i>
                    <h3 class="card-title">{{ lost_item_count }}</h3>
                    <p class="card-text">待领取失物</p>
                </div>
            </div>
        </div>
    </div>

    <!-- 学生管理 -->
    <div class="row mt-5">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-gradient-primary text-white">
                    <h5 class="mb-0"><i class="fas fa-user-graduate"></i> 学生信息管理</h5>
                </div>
                <div class="card-body">
                    {% if students %}
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="table-dark">
                                <tr>
                                    <th><i class="fas fa-id-card"></i> 学号</th>
                                    <th><i class="fas fa-user"></i> 用户名</th>
                                    <th><i class="fas fa-signature"></i> 姓名</th>
                                    <th><i class="fas fa-graduation-cap"></i> 专业</th>
                                    <th><i class="fas fa-users"></i> 班级</th>
                                    <th><i class="fas fa-tools"></i> 操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for student in students %}
                                <tr class="student-row">
                                    <td><span class="badge bg-primary">{{ student.student_id }}</span></td>
                                    <td>{{ student.username }}</td>
                                    <td>{{ student.name }}</td>
                                    <td>{{ student.major }}</td>
                                    <td>{{ student.class_name }}</td>
                                    <td>
                                        <button class="btn btn-sm btn-outline-info" onclick="viewStudent('{{ student.id }}')"></button>
                                            <i class="fas fa-eye"></i> 查看
                                        </button>
                                        <button class="btn btn-sm btn-outline-warning" onclick="editStudent('{{ student.id }}')"></button>
                                            <i class="fas fa-edit"></i> 编辑
                                        </button>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-user-slash fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">暂无学生数据</h5>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- 快速操作 -->
    <div class="row mt-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-gradient-secondary text-white">
                    <h5 class="mb-0"><i class="fas fa-bolt"></i> 快速操作</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-3 mb-2">
                            <a href="{{ url_for('news') }}" class="btn btn-outline-primary btn-block w-100">
                                <i class="fas fa-newspaper"></i> 管理新闻
                            </a>
                        </div>
                        <div class="col-md-3 mb-2">
                            <a href="{{ url_for('admin_seat_management') }}" class="btn btn-outline-success btn-block w-100">
                                <i class="fas fa-chair"></i> 座位管理
                            </a>
                        </div>
                        <div class="col-md-3 mb-2">
                            <a href="{{ url_for('lost_found') }}" class="btn btn-outline-warning btn-block w-100">
                                <i class="fas fa-search"></i> 失物管理
                            </a>
                        </div>
                        <div class="col-md-3 mb-2">
                            <a href="{{ url_for('admin_face_recognition_page') }}" class="btn btn-outline-danger btn-block w-100">
                                <i class="fas fa-camera"></i> 人脸识别入馆
                            </a>
                        </div>
                        <div class="col-md-3 mb-2">
                            <a href="{{ url_for('admin_face_recognition_exit') }}" class="btn btn-outline-warning btn-block w-100">
                                <i class="fas fa-user-times"></i> 人脸识别离馆
                            </a>
                        </div>
                        <div class="col-md-3 mb-2">
                            <button class="btn btn-outline-success btn-block w-100" onclick="showAddStudentModal()">
                                <i class="fas fa-user-plus"></i> 添加学生
                            </button>
                        </div>
                        <div class="col-md-3 mb-2">
                            <button class="btn btn-outline-info btn-block w-100" onclick="refreshData()">
                                <i class="fas fa-sync-alt"></i> 刷新数据
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 学生详情模态框 -->
<div class="modal fade" id="studentModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title"><i class="fas fa-user"></i> 学生详情</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body" id="studentDetails">
                <!-- 学生详情内容将通过JavaScript加载 -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<!-- 添加学生模态框 -->
<div class="modal fade" id="addStudentModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-success text-white">
                <h5 class="modal-title"><i class="fas fa-user-plus"></i> 添加新学生</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form id="addStudentForm" action="{{ url_for('add_student') }}" method="POST">
                    <div class="mb-3">
                        <label for="addUsername" class="form-label">用户名 <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="addUsername" name="username" required>
                        <div class="form-text">用于登录系统的用户名，必须唯一</div>
                    </div>
                    <div class="mb-3">
                        <label for="addStudentId" class="form-label">学号 <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="addStudentId" name="student_id" required>
                        <div class="form-text">学生的唯一学号</div>
                    </div>
                    <div class="mb-3">
                        <label for="addName" class="form-label">姓名 <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="addName" name="name" required>
                    </div>
                    <div class="mb-3">
                        <label for="addMajor" class="form-label">专业 <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="addMajor" name="major" required>
                    </div>
                    <div class="mb-3">
                        <label for="addClass" class="form-label">班级 <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="addClass" name="class_name" required>
                    </div>
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle"></i> 新学生的默认密码为：<strong>123456</strong>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-success" onclick="submitAddStudent()">添加学生</button>
            </div>
        </div>
    </div>
</div>

<!-- 编辑学生模态框 -->
<div class="modal fade" id="editStudentModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-warning text-white">
                <h5 class="modal-title"><i class="fas fa-edit"></i> 编辑学生信息</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form id="editStudentForm">
                    <div class="mb-3">
                        <label for="editName" class="form-label">姓名</label>
                        <input type="text" class="form-control" id="editName" required>
                    </div>
                    <div class="mb-3">
                        <label for="editMajor" class="form-label">专业</label>
                        <input type="text" class="form-control" id="editMajor" required>
                    </div>
                    <div class="mb-3">
                        <label for="editClass" class="form-label">班级</label>
                        <input type="text" class="form-control" id="editClass" required>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-warning" onclick="saveStudent()">保存修改</button>
            </div>
        </div>
    </div>
</div>

<style>
/* 管理面板标题样式 */
.admin-title {
    color: #2c3e50;
    font-weight: 700;
    font-size: 2.5rem;
    margin-bottom: 0.5rem;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    background: linear-gradient(135deg, #667eea, #764ba2);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.admin-title i {
    background: linear-gradient(135deg, #667eea, #764ba2);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-right: 0.5rem;
}

.admin-subtitle {
    color: #6c757d;
    font-size: 1.2rem;
    font-weight: 500;
    margin-bottom: 2rem;
}

.bg-gradient-primary {
    background: linear-gradient(45deg, #007bff, #0056b3);
}

.bg-gradient-secondary {
    background: linear-gradient(45deg, #6c757d, #495057);
}

.student-row {
    transition: all 0.3s ease;
}

.student-row:hover {
    background-color: #f8f9fa;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.card {
    border: none;
    border-radius: 15px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
}

.card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.btn {
    border-radius: 8px;
    transition: all 0.3s ease;
}

.btn:hover {
    transform: translateY(-2px);
}

.table th {
    border-top: none;
    font-weight: 600;
}

.badge {
    font-size: 0.9em;
}

.modal-content {
    border-radius: 15px;
    border: none;
}

.modal-header {
    border-radius: 15px 15px 0 0;
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(30px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.card {
    animation: fadeInUp 0.6s ease-out;
}

.card:nth-child(1) { animation-delay: 0.1s; }
.card:nth-child(2) { animation-delay: 0.2s; }
.card:nth-child(3) { animation-delay: 0.3s; }
</style>

<script>
let currentStudentId = null;

function viewStudent(studentId) {
    // 这里可以添加AJAX请求获取学生详细信息
    const studentDetails = document.getElementById('studentDetails');
    studentDetails.innerHTML = `
        <div class="text-center">
            <div class="spinner-border text-primary" role="status">
                <span class="visually-hidden">加载中...</span>
            </div>
            <p class="mt-2">正在加载学生详情...</p>
        </div>
    `;
    
    const modal = new bootstrap.Modal(document.getElementById('studentModal'));
    modal.show();
    
    // 模拟加载延迟
    setTimeout(() => {
        studentDetails.innerHTML = `
            <div class="row">
                <div class="col-md-6">
                    <p><strong>学号：</strong> <span class="badge bg-primary">STU${studentId.toString().padStart(3, '0')}</span></p>
                    <p><strong>姓名：</strong> 学生姓名</p>
                    <p><strong>专业：</strong> 计算机科学</p>
                </div>
                <div class="col-md-6">
                    <p><strong>班级：</strong> 计科2021-1班</p>
                    <p><strong>注册时间：</strong> 2021-09-01</p>
                    <p><strong>状态：</strong> <span class="badge bg-success">正常</span></p>
                </div>
            </div>
        `;
    }, 1000);
}

function editStudent(studentId) {
    currentStudentId = studentId;
    const modal = new bootstrap.Modal(document.getElementById('editStudentModal'));
    modal.show();
    
    // 这里可以预填充学生信息
    document.getElementById('editName').value = '学生姓名';
    document.getElementById('editMajor').value = '计算机科学';
    document.getElementById('editClass').value = '计科2021-1班';
}

function saveStudent() {
    const name = document.getElementById('editName').value;
    const major = document.getElementById('editMajor').value;
    const className = document.getElementById('editClass').value;
    
    if (!name || !major || !className) {
        alert('请填写完整信息');
        return;
    }
    
    // 这里可以添加AJAX请求保存学生信息
    alert('学生信息已更新（演示功能）');
    
    const modal = bootstrap.Modal.getInstance(document.getElementById('editStudentModal'));
    modal.hide();
}

function showAddStudentModal() {
    // 清空表单
    document.getElementById('addStudentForm').reset();
    
    const modal = new bootstrap.Modal(document.getElementById('addStudentModal'));
    modal.show();
}

function submitAddStudent() {
    const form = document.getElementById('addStudentForm');
    const formData = new FormData(form);
    
    // 验证表单
    const username = formData.get('username');
    const studentId = formData.get('student_id');
    const name = formData.get('name');
    const major = formData.get('major');
    const className = formData.get('class_name');
    
    if (!username || !studentId || !name || !major || !className) {
        alert('请填写所有必填字段');
        return;
    }
    
    // 验证用户名格式（只允许字母数字下划线）
    if (!/^[a-zA-Z0-9_]+$/.test(username)) {
        alert('用户名只能包含字母、数字和下划线');
        return;
    }
    
    // 验证学号格式
    if (!/^[A-Z0-9]+$/.test(studentId)) {
        alert('学号格式不正确，请使用大写字母和数字');
        return;
    }
    
    // 提交表单
    form.submit();
}

function refreshData() {
    // 刷新页面数据
    location.reload();
}

// 页面加载完成后的初始化
document.addEventListener('DOMContentLoaded', function() {
    // 添加表格行的动画效果
    const rows = document.querySelectorAll('.student-row');
    rows.forEach((row, index) => {
        row.style.animationDelay = `${0.1 * index}s`;
        row.style.animation = 'fadeInUp 0.6s ease-out forwards';
    });
});
</script>
{% endblock %}